﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DungeonCraft.Engine.Interfaces;
using Microsoft.Xna.Framework;
using DungeonCraft.DataModel.Tools;

namespace DungeonCraft.Engine
{
    public class WireframeAnimationManager : IAnimationManager
    {

        public WireframeAnimationManager()
        {
            CallbackTimer timer = new CallbackTimer();
            timer.tickTime = 150;
            timer.TickCallBack = getNextWalkingFrame;
            timer.Start();
        }

        private int currentWalkingFrame = 0;
        public Microsoft.Xna.Framework.Rectangle GetCurrentPlayerAnimationFrame(DataModel.Player player)
        {
            Rectangle frame;
            if (!player.Jumping)
                switch (player.Direction)
                {
                    case DataModel.Player.Directions.Down:
                        switch (currentWalkingFrame)
                        {
                            case 0:
                                frame = new Rectangle(0, 0, 19, 29);
                                break;
                            case 1:
                                frame = new Rectangle(19, 0, 19, 29);
                                break;
                            case 2:
                                frame = new Rectangle(38, 0, 19, 29);
                                break;
                            default:
                                frame = new Rectangle(0, 0, 19, 29);
                                break;
                        }
                        break;
                    case DataModel.Player.Directions.Up:
                        switch (currentWalkingFrame)
                        {
                            case 0:
                                frame = new Rectangle(132, 0, 19, 29);
                                break;
                            case 1:
                                frame = new Rectangle(151, 0, 19, 29);
                                break;
                            case 2:
                                frame = new Rectangle(113, 0, 19, 29);
                                break;
                            default:
                                frame = new Rectangle(151, 0, 19, 29);
                                break;
                        }
                        
                        break;
                    case DataModel.Player.Directions.Left:
                        switch (currentWalkingFrame)
                        {
                            case 0:
                                frame = new Rectangle(58, 0, 19, 29);
                                break;
                            case 1:
                                frame = new Rectangle(77, 0, 19, 29);
                                break;
                            case 2:
                                frame = new Rectangle(96, 0, 19, 29);
                                break;
                            default:
                                frame = new Rectangle(58, 0, 19, 29);
                                break;
                        }
                        
                        break;
                    case DataModel.Player.Directions.Right:
                        switch (currentWalkingFrame)
                        {
                            case 0:
                                frame = new Rectangle(77, 0, -19, 29);
                                break;
                            case 1:
                                frame = new Rectangle(96, 0, -19, 29);
                                break;
                            case 2:
                                frame = new Rectangle(115, 0, -19, 29);
                                break;
                            default:
                                frame = new Rectangle(58, 0, -19, 29);
                                break;
                        }
                        break;
                    default:
                        frame = new Rectangle(0, 0, 19, 29);
                        break;
                }
            else
                switch (player.Direction)
                {
                    case DataModel.Player.Directions.Down:
                        frame = new Rectangle(42, 62, 19, 29);
                        break;
                    case DataModel.Player.Directions.Up:
                        frame = new Rectangle(160, 62, 19, 29);
                        break;
                    case DataModel.Player.Directions.Left:
                        frame = new Rectangle(95, 0, 19, 29);
                        break;
                    case DataModel.Player.Directions.Right:
                        frame = new Rectangle(172, 0, 19, 29);
                        break;
                    default:
                        frame = new Rectangle(42, 62, 19, 29);
                        break;
                }
                
            return frame;
        }

        private void getNextWalkingFrame()
        {
            currentWalkingFrame++;
            if (currentWalkingFrame == 3)
                currentWalkingFrame = 0;
        }
    }
}
